********************************************************************************
* FAC AUGMENTED TAYLOR RULE ANALYSIS v6
* With Marginal Effects (Brambor et al. 2006), Multiple Lags, Robustness Checks
* Authors: O'Flynn & Stockhammer
********************************************************************************

clear all
set more off

* Load data
use "fac_fred_merged.dta", clear

* Set time series
tsset time_trend

* Drop and recreate yq to ensure correct type
capture drop yq
gen yq = yq(year, quarter)
format yq %tq

********************************************************************************
* SETUP: Create Additional Variables
********************************************************************************

* Drop existing variables if re-running
capture drop precrisis postcrisis phase pre_financial pre_recession
capture drop glb_era volcker_era zlb_period era
capture drop fac_lag2 fac_lag3 fac_lag4
capture drop precrisis4 postcrisis8
capture drop d_ffr d_fac d_inflation d_output

* ============================================================================
* STRUCTURAL BREAK INDICATORS
* ============================================================================

* Volcker era (Oct 1979 onwards)
gen volcker_era = (year > 1979) | (year == 1979 & quarter == 4)
label var volcker_era "Post-Volcker (1979Q4+)"

* Gramm-Leach-Bliley Act (Nov 1999)
gen glb_era = (year >= 2000)
label var glb_era "Post-GLB (2000+)"

* Zero Lower Bound periods
gen zlb_period = 0
replace zlb_period = 1 if yq >= yq(2008,4) & yq <= yq(2015,4)
replace zlb_period = 1 if yq >= yq(2020,1) & yq <= yq(2022,1)
label var zlb_period "Zero Lower Bound period"

* Four-era variable
gen era = 1 if year < 1980
replace era = 2 if year >= 1980 & year < 2000
replace era = 3 if year >= 2000 & year < 2008
replace era = 4 if year >= 2008
label define era_lbl 1 "Pre-Volcker" 2 "Volcker-Greenspan" 3 "Pre-GFC" 4 "Post-GFC"
label values era era_lbl

* ============================================================================
* CRISIS PHASE INDICATORS (10 crises)
* ============================================================================

gen precrisis = 0
gen postcrisis = 0

* 1970 recession
replace precrisis = 1 if yq >= yq(1968,2) & yq <= yq(1969,3)
replace postcrisis = 1 if yq >= yq(1971,1) & yq <= yq(1972,2)

* Stagflation 1973-75
replace precrisis = 1 if yq >= yq(1972,2) & yq <= yq(1973,3)
replace postcrisis = 1 if yq >= yq(1975,2) & yq <= yq(1976,3)

* 1980 recession
replace precrisis = 1 if yq >= yq(1978,3) & yq <= yq(1979,4)
replace postcrisis = 1 if yq >= yq(1980,4) & yq <= yq(1982,1)

* Volcker shock
replace precrisis = 1 if yq >= yq(1980,1) & yq <= yq(1981,2)
replace postcrisis = 1 if yq >= yq(1983,1) & yq <= yq(1984,2)

* 1987 crash
replace precrisis = 1 if yq >= yq(1986,2) & yq <= yq(1987,3)
replace postcrisis = 1 if yq >= yq(1988,1) & yq <= yq(1989,2)

* 1990 recession
replace precrisis = 1 if yq >= yq(1989,1) & yq <= yq(1990,2)
replace postcrisis = 1 if yq >= yq(1991,2) & yq <= yq(1992,3)

* LTCM 1998
replace precrisis = 1 if yq >= yq(1997,1) & yq <= yq(1998,2)
replace postcrisis = 1 if yq >= yq(1999,1) & yq <= yq(2000,2)

* Dotcom 2001
replace precrisis = 1 if yq >= yq(1999,3) & yq <= yq(2000,4)
replace postcrisis = 1 if yq >= yq(2002,1) & yq <= yq(2003,2)

* GFC 2008
replace precrisis = 1 if yq >= yq(2006,1) & yq <= yq(2008,2)
replace postcrisis = 1 if yq >= yq(2009,3) & yq <= yq(2011,4)

* COVID 2020
replace precrisis = 1 if yq >= yq(2018,3) & yq <= yq(2019,4)
replace postcrisis = 1 if yq >= yq(2020,3) & yq <= yq(2021,4)

* Phase variable
gen phase = 0
replace phase = 1 if precrisis == 1
replace phase = 2 if crisis_any == 1
replace phase = 3 if postcrisis == 1
label define phase_lbl 0 "Normal" 1 "Pre-Crisis" 2 "Crisis" 3 "Post-Crisis"
label values phase phase_lbl

* Financial vs recession crises (pre-periods)
gen pre_financial = 0
replace pre_financial = 1 if yq >= yq(1986,2) & yq <= yq(1987,3)
replace pre_financial = 1 if yq >= yq(1997,1) & yq <= yq(1998,2)
replace pre_financial = 1 if yq >= yq(2006,1) & yq <= yq(2008,2)
label var pre_financial "Pre-financial crisis period"

gen pre_recession = precrisis == 1 & pre_financial == 0
label var pre_recession "Pre-recession period (non-financial)"

* Create additional lags
gen fac_lag2 = L2.fac_score
gen fac_lag3 = L3.fac_score
gen fac_lag4 = L4.fac_score
label var fac_lag2 "FAC score (t-2)"
label var fac_lag3 "FAC score (t-3)"
label var fac_lag4 "FAC score (t-4)"

* Summary stats
tab phase
tab era
tab zlb_period

********************************************************************************
* PART 1: OPTIMAL LAG SELECTION
********************************************************************************

* Lag 1
reg ffr inflation_gap output_gap fac_score_lag1
local aic1 = -2*e(ll) + 2*e(rank)
local bic1 = -2*e(ll) + e(rank)*ln(e(N))
outreg2 using "lag_selection.doc", replace ctitle("Lag 1") dec(3) addstat(AIC, `aic1', BIC, `bic1')

* Lag 2
reg ffr inflation_gap output_gap fac_lag2
local aic2 = -2*e(ll) + 2*e(rank)
local bic2 = -2*e(ll) + e(rank)*ln(e(N))
outreg2 using "lag_selection.doc", append ctitle("Lag 2") dec(3) addstat(AIC, `aic2', BIC, `bic2')

* Lag 3
reg ffr inflation_gap output_gap fac_lag3
local aic3 = -2*e(ll) + 2*e(rank)
local bic3 = -2*e(ll) + e(rank)*ln(e(N))
outreg2 using "lag_selection.doc", append ctitle("Lag 3") dec(3) addstat(AIC, `aic3', BIC, `bic3')

* Lag 4
reg ffr inflation_gap output_gap fac_lag4
local aic4 = -2*e(ll) + 2*e(rank)
local bic4 = -2*e(ll) + e(rank)*ln(e(N))
outreg2 using "lag_selection.doc", append ctitle("Lag 4") dec(3) addstat(AIC, `aic4', BIC, `bic4')

* Lags 1-2
reg ffr inflation_gap output_gap fac_score_lag1 fac_lag2
local aic12 = -2*e(ll) + 2*e(rank)
local bic12 = -2*e(ll) + e(rank)*ln(e(N))
outreg2 using "lag_selection.doc", append ctitle("Lags 1-2") dec(3) addstat(AIC, `aic12', BIC, `bic12')

* Lags 1-4
reg ffr inflation_gap output_gap fac_score_lag1 fac_lag2 fac_lag3 fac_lag4
local aic14 = -2*e(ll) + 2*e(rank)
local bic14 = -2*e(ll) + e(rank)*ln(e(N))
outreg2 using "lag_selection.doc", append ctitle("Lags 1-4") dec(3) addstat(AIC, `aic14', BIC, `bic14')

di "=========================================="
di "LAG SELECTION SUMMARY"
di "=========================================="
di "Lag 1: AIC = " `aic1' " BIC = " `bic1'
di "Lag 2: AIC = " `aic2' " BIC = " `bic2'
di "Lag 3: AIC = " `aic3' " BIC = " `bic3'
di "Lag 4: AIC = " `aic4' " BIC = " `bic4'
di "Lags 1-2: AIC = " `aic12' " BIC = " `bic12'
di "Lags 1-4: AIC = " `aic14' " BIC = " `bic14'
di "=========================================="

********************************************************************************
* PART 2: MAIN MODELS WITH ALTERNATIVE STANDARD ERRORS
********************************************************************************

* OLS
reg ffr inflation_gap output_gap fac_score_lag1
outreg2 using "main_models_se.doc", replace ctitle("OLS") dec(3)

* Newey-West (4 lags)
newey ffr inflation_gap output_gap fac_score_lag1, lag(4)
outreg2 using "main_models_se.doc", append ctitle("NW-4") dec(3)

* Newey-West (8 lags)
newey ffr inflation_gap output_gap fac_score_lag1, lag(8)
outreg2 using "main_models_se.doc", append ctitle("NW-8") dec(3)

* Newey-West (12 lags)
newey ffr inflation_gap output_gap fac_score_lag1, lag(12) force
outreg2 using "main_models_se.doc", append ctitle("NW-12") dec(3)

********************************************************************************
* PART 3: PHASE ANALYSIS
********************************************************************************

* By phase (separate regressions)
reg ffr inflation_gap output_gap fac_score_lag1 if phase == 0
outreg2 using "phase_analysis.doc", replace ctitle("Normal") dec(3)

reg ffr inflation_gap output_gap fac_score_lag1 if phase == 1
outreg2 using "phase_analysis.doc", append ctitle("Pre-Crisis") dec(3)

reg ffr inflation_gap output_gap fac_score_lag1 if phase == 2
outreg2 using "phase_analysis.doc", append ctitle("Crisis") dec(3)

reg ffr inflation_gap output_gap fac_score_lag1 if phase == 3
outreg2 using "phase_analysis.doc", append ctitle("Post-Crisis") dec(3)

* Interaction model
reg ffr inflation_gap output_gap c.fac_score_lag1##i.phase
outreg2 using "phase_analysis.doc", append ctitle("Interaction") dec(3)

********************************************************************************
* PART 4: MARGINAL EFFECTS (Brambor et al. 2006)
********************************************************************************

* Phase interaction with marginal effects
reg ffr inflation_gap output_gap c.fac_score_lag1##i.phase
margins, dydx(fac_score_lag1) at(phase=(0 1 2 3))
marginsplot, title("Marginal Effect of FAC by Phase") ///
    xtitle("Phase") ytitle("Marginal Effect on FFR") ///
    xlabel(0 "Normal" 1 "Pre-Crisis" 2 "Crisis" 3 "Post-Crisis") ///
    ylabel(, angle(horizontal)) ///
    note("95% CIs. Brambor et al. (2006) specification.")
graph export "me_phase.png", replace

* Crisis interaction
reg ffr inflation_gap output_gap c.fac_score_lag1##c.crisis_any
margins, dydx(fac_score_lag1) at(crisis_any=(0 1))
marginsplot, title("Marginal Effect of FAC: Normal vs Crisis") ///
    xtitle("Crisis") ytitle("Marginal Effect on FFR") ///
    xlabel(0 "Normal" 1 "Crisis") ///
    ylabel(, angle(horizontal))
graph export "me_crisis.png", replace

* ZLB interaction
reg ffr inflation_gap output_gap c.fac_score_lag1##c.zlb_period
margins, dydx(fac_score_lag1) at(zlb_period=(0 1))
marginsplot, title("Marginal Effect of FAC: Normal vs ZLB") ///
    xtitle("ZLB Period") ytitle("Marginal Effect on FFR") ///
    xlabel(0 "Normal" 1 "ZLB") ///
    ylabel(, angle(horizontal))
graph export "me_zlb.png", replace

* GLB interaction
reg ffr inflation_gap output_gap c.fac_score_lag1##c.glb_era
margins, dydx(fac_score_lag1) at(glb_era=(0 1))
marginsplot, title("Marginal Effect of FAC: Pre vs Post GLB") ///
    xtitle("GLB Era") ytitle("Marginal Effect on FFR") ///
    xlabel(0 "Pre-GLB" 1 "Post-GLB") ///
    ylabel(, angle(horizontal))
graph export "me_glb.png", replace

* Volcker interaction
reg ffr inflation_gap output_gap c.fac_score_lag1##c.volcker_era
margins, dydx(fac_score_lag1) at(volcker_era=(0 1))
marginsplot, title("Marginal Effect of FAC: Pre vs Post Volcker") ///
    xtitle("Volcker Era") ytitle("Marginal Effect on FFR") ///
    xlabel(0 "Pre-Volcker" 1 "Post-Volcker") ///
    ylabel(, angle(horizontal))
graph export "me_volcker.png", replace

********************************************************************************
* PART 5: STRUCTURAL BREAKS - ERAS
********************************************************************************

* Four-era analysis
reg ffr inflation_gap output_gap fac_score_lag1 if era == 1
outreg2 using "era_analysis.doc", replace ctitle("Pre-Volcker") dec(3)

reg ffr inflation_gap output_gap fac_score_lag1 if era == 2
outreg2 using "era_analysis.doc", append ctitle("Volcker-Greenspan") dec(3)

reg ffr inflation_gap output_gap fac_score_lag1 if era == 3
outreg2 using "era_analysis.doc", append ctitle("Pre-GFC 2000-07") dec(3)

reg ffr inflation_gap output_gap fac_score_lag1 if era == 4
outreg2 using "era_analysis.doc", append ctitle("Post-GFC") dec(3)

* Pre/Post GLB
reg ffr inflation_gap output_gap fac_score_lag1 if glb_era == 0
outreg2 using "glb_analysis.doc", replace ctitle("Pre-GLB 1968-99") dec(3)

reg ffr inflation_gap output_gap fac_score_lag1 if glb_era == 1
outreg2 using "glb_analysis.doc", append ctitle("Post-GLB 2000-25") dec(3)

* Pre/Post Volcker
reg ffr inflation_gap output_gap fac_score_lag1 if volcker_era == 0
outreg2 using "volcker_analysis.doc", replace ctitle("Pre-Volcker") dec(3)

reg ffr inflation_gap output_gap fac_score_lag1 if volcker_era == 1
outreg2 using "volcker_analysis.doc", append ctitle("Post-Volcker") dec(3)

********************************************************************************
* PART 6: CRISIS TYPE - FINANCIAL VS RECESSION
********************************************************************************

reg ffr inflation_gap output_gap fac_score_lag1 if pre_financial == 1
outreg2 using "crisis_type.doc", replace ctitle("Pre-Financial") dec(3)

reg ffr inflation_gap output_gap fac_score_lag1 if pre_recession == 1
outreg2 using "crisis_type.doc", append ctitle("Pre-Recession") dec(3)

********************************************************************************
* PART 7: ZLB ROBUSTNESS
********************************************************************************

reg ffr inflation_gap output_gap fac_score_lag1 if zlb_period == 0
outreg2 using "zlb_robustness.doc", replace ctitle("Non-ZLB") dec(3)

reg ffr inflation_gap output_gap fac_score_lag1 if zlb_period == 1
outreg2 using "zlb_robustness.doc", append ctitle("ZLB Only") dec(3)

********************************************************************************
* PART 8: DISTRIBUTED LAG MODEL
********************************************************************************

reg ffr inflation_gap output_gap fac_score_lag1 fac_lag2 fac_lag3 fac_lag4
outreg2 using "distributed_lags.doc", replace ctitle("All Lags") dec(3)

* Joint significance test
test fac_score_lag1 fac_lag2 fac_lag3 fac_lag4
local joint_p = r(p)
di "Joint F-test p-value: " `joint_p'

* Cumulative effect
lincom fac_score_lag1 + fac_lag2 + fac_lag3 + fac_lag4
local cum_effect = r(estimate)
local cum_se = r(se)
di "Cumulative effect: " `cum_effect' " SE: " `cum_se'

********************************************************************************
* PART 9: GRANGER CAUSALITY
********************************************************************************

* VAR model
var ffr fac_score, lags(1/4)
vargranger

********************************************************************************
* PART 10: FIRST DIFFERENCES (Stationarity)
********************************************************************************

gen d_ffr = D.ffr
gen d_fac = D.fac_score
gen d_inflation = D.inflation_gap
gen d_output = D.output_gap

reg d_ffr d_inflation d_output L.d_fac
outreg2 using "first_diff.doc", replace ctitle("First Diff") dec(3)

********************************************************************************
* PART 11: ALTERNATIVE CRISIS WINDOWS
********************************************************************************

* 4-quarter pre-crisis windows (GFC only for illustration)
gen precrisis4 = 0
replace precrisis4 = 1 if yq >= yq(2007,3) & yq <= yq(2008,2)

* 8-quarter pre-crisis windows
gen precrisis8 = 0
replace precrisis8 = 1 if yq >= yq(2006,3) & yq <= yq(2008,2)

reg ffr inflation_gap output_gap fac_score_lag1 if precrisis4 == 1
outreg2 using "window_robustness.doc", replace ctitle("4Q Pre-GFC") dec(3)

reg ffr inflation_gap output_gap fac_score_lag1 if precrisis8 == 1
outreg2 using "window_robustness.doc", append ctitle("8Q Pre-GFC") dec(3)

reg ffr inflation_gap output_gap fac_score_lag1 if precrisis == 1
outreg2 using "window_robustness.doc", append ctitle("6Q All Crises") dec(3)

********************************************************************************
* SAVE
********************************************************************************

save "fac_analysis_v6.dta", replace

di "========================================"
di "ANALYSIS COMPLETE"
di "========================================"
di "Output files created:"
di "  - lag_selection.doc"
di "  - main_models_se.doc"
di "  - phase_analysis.doc"
di "  - era_analysis.doc"
di "  - glb_analysis.doc"
di "  - volcker_analysis.doc"
di "  - crisis_type.doc"
di "  - zlb_robustness.doc"
di "  - distributed_lags.doc"
di "  - first_diff.doc"
di "  - window_robustness.doc"
di "Graphs:"
di "  - me_phase.png"
di "  - me_crisis.png"
di "  - me_zlb.png"
di "  - me_glb.png"
di "  - me_volcker.png"
di "========================================"

********************************************************************************
